﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Tank90.Properties;

namespace Tank90
{
    public partial class CustomMap : Form
    {
        public CustomMap()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var saveFileDialog = new SaveFileDialog();
            Thread saveThread = null;
            saveFileDialog.DefaultExt = "xlsx";
            saveFileDialog.Filter = "Excel 2007文件|*.xlsx|Excel 99-03文件|*.xls";
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                var frmProgress = new frmProgressBar();

                saveThread = new Thread(SaveData);
                object paraMeter = new object[]
                {
                    saveFileDialog.FileName,
                    frmProgress
                };

                saveThread.Start(paraMeter);
            }
        }

        [STAThread]
        private void SaveData(object paraMeter)
        {
            XSSFWorkbook xssfWorkbook = WriteExcel((frmProgressBar) ((object[]) paraMeter)[1]);
            var memoryStream = new MemoryStream();
            xssfWorkbook.Write(memoryStream);
            string tmp = ((object[]) paraMeter)[0].ToString();
            File.WriteAllBytes(((object[]) paraMeter)[0].ToString(), memoryStream.ToArray());
        }

        private XSSFWorkbook WriteExcel(frmProgressBar frmProgress)
        {
            int totalCount = customMapPanel1.maps.Count;
            frmProgress.Show();
            frmProgress.totalValue = totalCount;
            Thread.Sleep(100);
            List<Map> maps = customMapPanel1.maps;
            var xssfWorkbook = new XSSFWorkbook();
            var xssfSheet = (XSSFSheet) xssfWorkbook.CreateSheet("sheet1");
            IRow titleRow = xssfSheet.CreateRow(0);
            titleRow.CreateCell(0).SetCellValue("ID");
            titleRow.CreateCell(1).SetCellValue("ElementTypeVch");
            titleRow.CreateCell(2).SetCellValue("LocationX");
            titleRow.CreateCell(3).SetCellValue("LocationY");
            titleRow.CreateCell(4).SetCellValue("CheckID");
            for (int i = 0; i < totalCount; i++)
            {
                IRow row = xssfSheet.CreateRow(i + 1);
                row.CreateCell(0).SetCellValue(i + 1);
                row.CreateCell(1).SetCellValue(maps[i].elemenType.ToString());
                row.CreateCell(2).SetCellValue(maps[i].x);
                row.CreateCell(3).SetCellValue(maps[i].y);
                row.CreateCell(4).SetCellValue(1);
                frmProgress.nowValue = i + 1;
            }
            return xssfWorkbook;
        }

        private void CustomMap_Load(object sender, EventArgs e)
        {
            picfStone.Image = Resources.fStone;
            picUStone.Image = Resources.vUStone;
            picDStone.Image = Resources.vDStone;
            picLStone.Image = Resources.hLTStone;
            picRStone.Image = Resources.hRTStone;
            //   this.picOStone.Image = Resources.oStone;

            picFBrick.Image = Resources.fBrick;
            picUBrick.Image = Resources.vUBrick;
            picDBrick.Image = Resources.vDBrick;
            picLBrick.Image = Resources.hLBrick;
            picRBrick.Image = Resources.hRBrick;
            //  this.picOBrick.Image = Resources.oBrick;

            picWater.Image = Resources.fWater;

            picGrass.Image = Resources.fGrass;

            picWhite.Image = Resources.fWhite;
            picClear.BackColor = Color.Snow;
            picClear.Tag = "清除";
        }

        private void pictureBox_Click(object sender, EventArgs e)
        {
            var pictureBox = (PictureBox) sender;
            switch (pictureBox.Name)
            {
                case "picfStone":
                    customMapPanel1.mapElemenType = MapElemenType.FStone;
                    break;
                case "picUStone":
                    customMapPanel1.mapElemenType = MapElemenType.UStone;
                    break;
                case "picDStone":
                    customMapPanel1.mapElemenType = MapElemenType.DStone;
                    break;
                case "picLStone":
                    customMapPanel1.mapElemenType = MapElemenType.LStone;
                    break;
                case "picRStone":
                    customMapPanel1.mapElemenType = MapElemenType.RStone;
                    break;
                    //case "picOStone":
                    //    this.customMapPanel1.mapElemenType = MapElemenType.OStone;
                    //    break;            
                case "picFBrick":
                    customMapPanel1.mapElemenType = MapElemenType.FBrick;
                    break;
                case "picUBrick":
                    customMapPanel1.mapElemenType = MapElemenType.UBrick;
                    break;
                case "picDBrick":
                    customMapPanel1.mapElemenType = MapElemenType.DBrick;
                    break;
                case "picLBrick":
                    customMapPanel1.mapElemenType = MapElemenType.RBrick;
                    break;
                case "picRBrick":
                    customMapPanel1.mapElemenType = MapElemenType.RBrick;
                    break;
                    //case "picOBrick":
                    //    this.customMapPanel1.mapElemenType = MapElemenType.OBrick;
                    //    break;             
                case "picWater":
                    customMapPanel1.mapElemenType = MapElemenType.Water;
                    break;
                case "picGrass":
                    customMapPanel1.mapElemenType = MapElemenType.Grass;
                    break;
                case "picWhite":
                    customMapPanel1.mapElemenType = MapElemenType.White;
                    break;

                    //case "":
                    //    this.customMapPanel1.mapElemenType = MapElemenType.Stone;
                    //    break;
                    //case "pictureBox2":
                    //    this.customMapPanel1.mapElemenType = MapElemenType.Brick;
                    //    break;
                    //case "pictureBox3":
                    //    this.customMapPanel1.mapElemenType = MapElemenType.River;
                    //    break;
                    //case "pictureBox4":
                    //    this.customMapPanel1.mapElemenType = MapElemenType.Grass;
                    //    break;
                    //case "pictureBox5":
                    //    this.customMapPanel1.mapElemenType = MapElemenType.White;
                    //    break;
                default:
                    customMapPanel1.mapElemenType = MapElemenType.None;
                    break;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            customMapPanel1.IsShowGrid = checkBox1.Checked;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void CustomMap_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
    }
}